package com.yz.jdd.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.yz.jdd.R;


public class PublicDialog extends Dialog {

    private TextView title;
    private AlxUrlTextView content;
    private Button ok;
    private Button cancel;
    private View line_center;//两按钮中间的竖线
    private View line;//横线
    //	private CheckBox check;
    private Boolean isCancelable = true;
    private Context mContext;

    /**
     * 普通dialog,带按钮的
     *
     * @param context
     */
    public PublicDialog(Context context) {
        super(context, R.style.Cate_Dialog);
        setContentView(R.layout.dialog_update);
        this.mContext = context;


    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.dialog_update);
        setCanceledOnTouchOutside(true);
//		WindowManager m = activity.getWindowManager();
//		Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.gravity = Gravity.CENTER;
        p.height = WindowManager.LayoutParams.WRAP_CONTENT;//(int) (d.getWidth() * 0.5);   //高度设置为屏幕的0.4
        p.width = WindowManager.LayoutParams.WRAP_CONTENT;//(int) (d.getWidth() * 0.85);    //宽度设置为屏幕的0.7
        getWindow().setAttributes(p);
    }

    public void setTitle(String stitle) {
        if (!TextUtils.isEmpty(stitle)) {
            title = (TextView) findViewById(R.id.title);
            title.setVisibility(View.VISIBLE);
            title.setText(stitle);

        }
    }


    public void setMessage(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            content = (AlxUrlTextView) findViewById(R.id.content);
            content.setGravity(Gravity.CENTER);

            content.setText(msg);

        }
    }

    public void setMessage(String msg, boolean isCenter) {
        if (!TextUtils.isEmpty(msg)) {
            content = (AlxUrlTextView) findViewById(R.id.content);
            if (isCenter) {
                content.setGravity(Gravity.CENTER);
            }
            content.setText(msg);
        }
    }

    public void setMessage(int msgId) {
        content = (AlxUrlTextView) findViewById(R.id.content);
        content.setText(msgId);


    }


    public void setRightBtn(String strOK, android.view.View.OnClickListener listener) {
        if (!TextUtils.isEmpty(strOK)) {
            ok = (Button) findViewById(R.id.btn_ok);
            ok.setText(strOK);
            ok.setVisibility(View.VISIBLE);
            ok.setOnClickListener(listener);
        }
    }


    public void setLeftBtn(String strRight, android.view.View.OnClickListener listener) {
        if (!TextUtils.isEmpty(strRight)) {
            View btn_divider = findViewById(R.id.btn_divider);
            btn_divider.setVisibility(View.VISIBLE);
            cancel = (Button) findViewById(R.id.btn_cancel);
            cancel.setText(strRight);
            cancel.setVisibility(View.VISIBLE);
            cancel.setOnClickListener(listener);
        }
    }


    @Override
    public void setCancelable(boolean cancelable) {
        isCancelable = cancelable;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!isCancelable) {
                return true;
            }
            this.dismiss();
        }
        return super.onKeyDown(keyCode, event);
    }


}
